package cn.star.jots.previewSort.service.impl;

import cn.star.jots.previewSort.mapper.PreviewSortMapper;
import cn.star.jots.previewSort.pojo.dto.PreviewSortQuery;
import cn.star.jots.previewSort.pojo.dto.PreviewSortSaveParam;
import cn.star.jots.previewSort.pojo.entity.PreviewSort;
import cn.star.jots.previewSort.pojo.vo.PreviewSortVO;
import cn.star.jots.previewSort.service.PreviewSortService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PreviewSortServiceImpl implements PreviewSortService {

    @Autowired
    PreviewSortMapper previewSortMapper;

    @Override
    public List<PreviewSortVO> selectAll(PreviewSortQuery previewSortQuery) {
        log.debug("查询预告片,参数:{}", previewSortQuery);
        return previewSortMapper.selectAll(previewSortQuery);
    }

    @Override
    public void save(PreviewSortSaveParam previewSortSaveParam) {
        log.debug("保存预告片信息:previewSortSaveParam={}", previewSortSaveParam);
        PreviewSort previewSort = new PreviewSort();
        BeanUtils.copyProperties(previewSortSaveParam, previewSort);
        if (previewSortSaveParam.getId() == null) {
            previewSort.setCreateTime(new Date());
            previewSortMapper.insert(previewSort);
        } else {
            log.debug("更新预告片信息:preview={}", previewSort);
            previewSort.setUpdateTime(new Date());
            previewSortMapper.update(previewSort);
        }

    }

    @Override
    public void delete(Long id) {
        log.debug("删除预告片信息:id={}", id);
        previewSortMapper.delete(id);
    }
}
